<!DOCTYPE HTML>
<html  lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-distLabel-add">
		<!-- 	<div class="form-group">	
				<label class="col-sm-3 control-label">分配批次：</label>
				<div class="col-sm-8">
					<input id="distNo" name="distNo" class="form-control" type="text">
				</div>
			</div> -->
		<!-- 	<div class="form-group">	
				<label class="col-sm-3 control-label">分配时间：</label>
				<div class="col-sm-8">
					<input id="distTime" name="distTime" class="form-control" type="text">
				</div>
			</div> -->
			<div class="form-group">	
				<label class="col-sm-3 control-label">厂商编号：</label>
				<div class="col-sm-8">
					<!-- <input id="gardenNo" name="gardenNo" class="form-control" type="text"> -->
					<select id="gardenNo" name="gardenNo"  class="form-control m-b" th:with="type=${@garden.getGarden()}">
	                    <option value="">---请选择---</option>
	                    <option th:each="garden : ${type}" th:text="${garden.gardenName+garden.teaGardenNo}" th:value="${garden.teaGardenNo}"></option>
	                </select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">入库批次号：</label>
				<div class="col-sm-8">
					<!-- <input id="depotHeadNo" name="depotHeadNo" class="form-control" type="text"> -->
					<select id="depotHeadNo" name="depotHeadNo"  class="form-control m-b" th:with="type=${@depotHead.getDepotHead()}">
	                    <option value="">---请选择---</option>
	                    <option th:each="depotHead : ${type}" th:text="${depotHead.depotHeadNo}" th:value="${depotHead.depotHeadNo}"></option>
	                </select>
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">起始序列号：</label>
				<div class="col-sm-8">
					<input id="beginSerialNumber" name="beginSerialNumber" class="form-control" type="text" readonly="readonly">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">分配数量：</label>
				<div class="col-sm-8">
					<input id="distNum" name="distNum" class="form-control" type="text">
				</div>
			</div>
			<div class="form-group">	
				<label class="col-sm-3 control-label">标签信息：</label>
				<div class="col-sm-8">
					<!-- <input id="bindingTagNo" name="bindingTagNo" class="form-control" type="text"> -->
					<select id=bindingTagNo name="bindingTagNo"  class="form-control m-b" disabled="disabled">
	                    <option value="">---请选择---</option>
	                </select>
				</div>
			</div>
			<!-- <div class="form-group">	
				<label class="col-sm-3 control-label">更新时间：</label>
				<div class="col-sm-8">
					<input id="updateDate" name="updateDate" class="form-control" type="text">
				</div>
			</div> -->
		<!-- 	<div class="form-group">	
				<label class="col-sm-3 control-label">绑定标识：</label>
				<div class="col-sm-8">
					<input id="bindingMark" name="bindingMark" class="form-control" type="text">
					<select id="bindingMark" name="bindingMark" class="form-control m-b" th:with="type=${@dict.getType('gh_binding_mark')}" disabled="disabled">
	                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
	                </select>
				</div>
			</div> -->
		<!-- 	<div class="form-group">	
				<label class="col-sm-3 control-label">绑定时间：</label>
				<div class="col-sm-8">
					<input id="bindingTime" name="bindingTime" class="form-control" type="text">
				</div>
			</div> -->
		
			<div class="form-group">	
				<label class="col-sm-3 control-label">备注：</label>
				<div class="col-sm-8">
					<input id="remark" name="remark" class="form-control" type="text">
				</div>
			</div>
		</form>
	</div>
    <div th:include="include::footer"></div>
    <script type="text/javascript">
		var prefix = ctx + "module/distLabel"
		$("#form-distLabel-add").validate({
			rules:{
				distNo:{
					required:true,
				},
				gardenNo:{
					required:true,
				},
				depotHeadNo:{
					required:true,
				},
				beginSerialNumber:{
					required:true,
				},
				distNum:{
					required:true,
					isDigits:true,
				},
				remark:{
					maxlength: 255
				},
			}
		});
		
		function submitHandler() {
	        if ($.validate.form()) {
	        	var distNum = $("#distNum").val();
	        	var placeholderStr = $("#distNum").attr("placeholder");
	        	var maxNum = placeholderStr.substring(placeholderStr.lastIndexOf(":")+1,placeholderStr.length);
	        	console.log("distNum :"+distNum);
	        	console.log("maxNum :"+maxNum);
	        	console.log(parseInt(distNum) < parseInt(maxNum));
	        	if(parseInt(distNum) > parseInt(maxNum)){
	        		 layer.open({
	        			  title: '提示信息',
	        			  content: '不能超过最大可分配数量 !',
	        			  icon:0
	        			});  
	        		return;
	        	}
	        	if(parseInt(distNum) == 0){
	        		 layer.open({
	        			  title: '提示信息',
	        			  content: '最小分配数量为1 !',
	        			  icon:0
	        			});  
	        		return;
	        	}
	        	layer.confirm('分配后将不可更改，确定要分配吗？',
	        			{ btn: ['确定','取消'],icon: 3, title:'提示'}, 
	        			function(index){ 
	        				layer.close(index); 
	        			 	$.operate.save(prefix + "/add", $('#form-distLabel-add').serialize());
	        			}, 
	        			function(index){ layer.close(index);});
	        }
	    }
		
		$("#depotHeadNo").change(function(){
		    $("#beginSerialNumber").val("");
		    $("#distNum").val("");
		    $("#distNum").removeAttr("placeholder");
		    var depotHeadNo = $("#depotHeadNo").find("option:selected").val(); 
			 if(depotHeadNo != "") { 
			     $.ajax({ 
			    	url:ctx +'module/depotHeadItem/getBeginSerialNumber', 
			    	type:"post",
			    	dataType:"json", 
			    	data: {depotHeadNo : $(this).val()}, 
			    	success: function (data) { 
			    		$("#beginSerialNumber").val(data[0]);
			    		$("#distNum").attr("placeholder","最大可分配数量:"+data.length);
			    		}, 
			    	error : function(request) {
	       				$.modal.alertError("系统错误");
	       				},
			    }); 
				 
			 }	
			});
		$("#gardenNo").change(function(){
			 $("#bindingTagNo").removeAttr("disabled");
			 var  gardenNo = $("#gardenNo").find("option:selected").val(); 
			 if(gardenNo == "") { 
				 $("#bindingTagNo").empty().append("<option value=''>---请选择---</option>"); 
				 $("#bindingTagNo").attr("disabled","disabled");
				 return false;
				 }
		    $("#bindingTagNo").empty().append("<option value=''>---请选择---</option>");
		     $.ajax({ 
		    	url:ctx +'module/tagInfo/getTagInfo', 
		    	type:"post",
		    	dataType:"json", 
		    	data: {teaGardenNo : $(this).val()}, 
		    	success: function (data) { 
		    		  for(var i = 0, len = data.length; i < len; i++) { 
		    			$("#bindingTagNo").append($("<option value='" + data[i].id + "'>" + data[i].tagInfo + "</option>")); 
		    			}  
		    		} , 
		    	error : function(request) {
       						$.modal.alertError("系统错误");
       				},
		    	}); 
		    	
			});
	</script>
</body>
</html>
